package se.ytterman.jpcap.framework.system;

import java.lang.*;
import java.io.*;
import java.util.*;

import javax.xml.xpath.*;
import org.xml.sax.*;
import org.w3c.dom.*;

import se.ytterman.jpcap.jni.PcapInterfaceFactory;
import se.ytterman.jpcap.jni.PcapInterface;
import se.ytterman.jpcap.jni.PcapSocket;

import se.ytterman.jpcap.data.bytearray.ArrayBinaryData;
import se.ytterman.jpcap.data.ethernet.MACAddress;
import se.ytterman.jpcap.data.ip.IPV4Address;

public class SystemConfiguration {

    // Static block to initialize interface array...
    static {
	if(true)
	    System.out.println("Detecting interfaces...");

	// Create Pcap interface factory 
	PcapInterfaceFactory factory = new PcapInterfaceFactory();
	
	// Fetch interface array
	// SystemConfiguration.systemInterfaces = factory.getInterfaceArray();

	// Create properties table...
	SystemConfiguration.systemProperties = new HashMap();
    }

    public static void parseSystemConfiguration(InputSource inputSource) {
	try {
	    // Create Xpath factory instance
	    XPathFactory xpathFactory = XPathFactory.newInstance();
	    
	    // Create an Xpath object
	    XPath xPath = xpathFactory.newXPath();
	    
	    // Create a nodeset of variable groups
	    String identifierExtractor = "/ProtocolConfiguration[@version=\"1.0\"]" +
		                         "/Interface[@version=\"1.0\"]" +
		                         "/InterfaceIdentification/Value";

	    Node interfaceIdentifier = (Node) xPath.evaluate(identifierExtractor,
							     inputSource,
							     XPathConstants.NODE);

	    // XXX Potential future use of several types...
	    String dataType = xPath.evaluate("@datatype", interfaceIdentifier);

	    // This string contains the MAC address...
	    String idString = interfaceIdentifier.getTextContent();

	    // XXX Make a MAC address object of it...
	    MACAddress selectedMAC = new MACAddress(idString);
	    
	    // XXX Iterate over the interfaces and ..
	    for(int i = 0; i < SystemConfiguration.systemInterfaces.length; i++) {
		// Extract present interface...
		PcapInterface presentInterface = SystemConfiguration.systemInterfaces[i];

		// Extract interface MAC address...
		MACAddress presentMAC = presentInterface.getMACAddress(); 

		// Extract the interface IP adresses...
		IPV4Address[] ipv4Addresses = presentInterface.getIPV4Addresses();
		
		// Compare to the wanted one and process...
		boolean equals = ArrayBinaryData.equals(selectedMAC, presentMAC);

		if(!equals) {
		    if(true)
			System.out.println(SystemConfiguration.systemInterfaces[i]);
		}
		else {
		    if(true)
			System.out.println(SystemConfiguration.systemInterfaces[i] + " <-");

		    // Set active interface
		    SystemConfiguration.activeInterface = presentInterface;

		    // Fill in system properties...
		    SystemConfiguration.systemProperties.put("INTERFACE_SRC_MAC", presentMAC);
		    SystemConfiguration.systemProperties.put("INTERFACE_SRC_IP" , ipv4Addresses[0]);
		}
	    }
	}
	catch(Exception e) {
	    System.out.println(e + " : " + e.getMessage());
	}
    }

    public static PcapInterface getActiveInterface() {
	return SystemConfiguration.activeInterface;
    }

    public static Object getSystemProperty(String propertyKey) {
	return SystemConfiguration.systemProperties.get(propertyKey);
    }

    // Array containing available interfaces
    private static PcapInterface systemInterfaces[];

    // Reference to active interface
    private static PcapInterface activeInterface;

    // Hashmap for system properties
    private static  HashMap <String, Object> systemProperties;
}
